package com.erp.app.admin.companyMenu.web;

//JRE Classes
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.admin.companyMenu.beans.CompanyMenuGroupVO;
import com.erp.app.admin.companyMenu.dao.CompanyMenuGroupSrchCriteria;
import com.erp.app.admin.companyMenu.ejb.CompanyMenuGroupPK;
import com.erp.app.common.ProjectConstants;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.action.SimpleSearchStyleAction;
import com.erp.framework.controller.form.SimpleSearchStyleForm;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;

public class CompanyMenuSearchAction extends SimpleSearchStyleAction {

    protected ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
        ActionForward actionForward = new ActionForward(mapping.getInput());

        CompanyMenuSearchForm form = (CompanyMenuSearchForm) formIn;

        return actionForward;
    }

    protected ActionForward processActionEdit(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors)

    {
        CompanyMenuSearchForm form = (CompanyMenuSearchForm) formIn;
        HttpSession session = request.getSession(false);

        ActionForward actionForward = new ActionForward(mapping.getInput());

        String editMenuId = form.getEditMenuId();
        String editGroupId = form.getEditGroupId();
        String editRoleGroupId = form.getEditRoleGroupId();
        String companyCode = WebUtil.getAppropriateCompanyCode(session, null);
        String editExistInd = form.getEditExistInd();

        CompanyMenuGroupPK pk = new CompanyMenuGroupPK(companyCode, editGroupId, editMenuId, editRoleGroupId);
//        System.out.println("companyCode:"+companyCode);
//        System.out.println("editGroupId:"+editGroupId);
//        System.out.println("editMenuId:"+editMenuId);
//        System.out.println("editRoleGroupId:"+editRoleGroupId);
        session.setAttribute("MENU_PRIMARY_KEYS", pk);
        session.setAttribute("MENU_EXIST_IND", editExistInd);
        System.out.println("SEARCH ACTION, editExistInd=" + editExistInd);

        if (ProjectConstants.GENERIC_YES.equals(editExistInd)) {
            try {
                SimpleSearchMaintStyleDelegate dg = getMasterDelegate(session);
                CompanyMenuGroupVO menuGroupVO = (CompanyMenuGroupVO) dg.findByPrimaryKey(new CompanyMenuGroupVO(), pk);

                System.out.println("SEARCH ACTION, menuGroupVO is null?" + (menuGroupVO == null));
                if (menuGroupVO != null) {
//                    System.out.println("************************Here is edit now!!!****************");
                    session.setAttribute(getSessionSimpleMaintStatus(), "edit");
                    session.setAttribute(getSessionSimpleMaintPk(), pk);
                    session.setAttribute(getSessionSimpleMaintEditType(), form.getEditType());
                } else {
//                    System.out.println("************************Here is create now!!!****************");

                    User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

                    SimpleBaseVo model = getInitialzedModel(form, editingUser);
                    if (model != null) {
                        session.setAttribute(getSessionSimpleMaintInitModel(), model);
                    }
                    session.setAttribute(getSessionSimpleMaintStatus(), "create");
                    session.setAttribute(getSessionSimpleMaintEditType(), form.getEditType());
                }
                System.out.println("SEARCH ACTION, getSessionSimpleMaintStatus()=" + getSessionSimpleMaintStatus());
                actionForward = mapping.findForward("edit");
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            }
        } else {
            System.out.println("THIS ENTRY DOES NOT EXIST IN DB.");

            session.setAttribute(getSessionSimpleMaintStatus(), "create");
            actionForward = mapping.findForward("create");
        }

        return actionForward;
    }

    protected String getFunctionId() {
        // struts-config xxxxx.do
        return "companyMenuSearch.do";
    }

    protected Class getMasterFormClass() {
        // the corresponding form class
        return CompanyMenuSearchForm.class;
    }

    protected Class getMasterVOClass() {
        // corresponding master vo class
        // return SystemMenuGroupVO.class;
        return CompanyMenuGroupVO.class;
    }

    protected Class getSearchCriteriaClass() {
        // corresponding searching criteria class
        return com.erp.app.admin.companyMenu.dao.CompanyMenuGroupSrchCriteria.class;
    }

    protected String getModuleName(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response) {
        return ProjectConstants.MOD_COMPANY_MENU_SEARCH;
    }

    protected boolean getLoadOnEnter() {
        return true;
    }

    protected boolean getResetOnEnter() {
        return false;
    }

    protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors, Object criteria) {
        HttpSession session = request.getSession(false);

        String isShare = WebUtil.getSysParamVal(session, ProjectConstants.SP_SHARE_COMPANY_MENU);

        // Company.out.println("$$$$$$$$ setOtherCriteria's companyCode = " +
        // WebUtil.getAppropriateCompanyCode(session, null));

        if (!ProjectConstants.GENERIC_YES.equals(isShare)) {
            String companyCode = WebUtil.getAppropriateCompanyCode(session, null);
            ((CompanyMenuGroupSrchCriteria) criteria).setSrchCompanyCode(companyCode);
        }

        CompanyMenuGroupSrchCriteria crit = (CompanyMenuGroupSrchCriteria) criteria;
        String first = request.getParameter("firstVisit");
        if (first != null && first.equals(GENERIC_YES)) {
            crit.setSrchGroupId(ProjectConstants.DBMENU_GENERAL_MENU);
            if ((crit.getSrchRoleGroupId() == null) || (crit.getSrchRoleGroupId().equals(""))) {
                crit.setSrchRoleGroupId(ProjectConstants.DBMENU_ADMIN_ROLE);
            }
        }
    }

    protected SimpleBaseVo getInitialzedModel(ActionForm formIn, User editingUser) {
        CompanyMenuSearchForm form = (CompanyMenuSearchForm) formIn;

        CompanyMenuGroupVO menuGroupVO = new CompanyMenuGroupVO();
        menuGroupVO.setCompanyCode(editingUser.getCompanyCode());
        menuGroupVO.setMenuId(form.getEditMenuId());
        menuGroupVO.setGroupId(form.getEditGroupId());
        menuGroupVO.setRoleGroupId(form.getEditRoleGroupId());
        menuGroupVO.setCreateUser(editingUser.getUserId());

        return menuGroupVO;
    }

    protected void initAction(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response) {

    }

    protected String getMasterFormSessionName() {
        return "companyMenuSearchForm";
    }

    protected SimpleBasePk getPk(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors){
        return null;
    }
	
    protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
        HttpServletResponse response, ActionErrors errors){
        SimpleSearchStyleForm form = (SimpleSearchStyleForm)formIn;
        form.setFormType(ProjectConstants.FORM_TYPE_SEARCH);
    }
    
}
